package com.ptswitch.material.pages.material;

import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.project.SplitPageInfo;
import org.slf4j.Logger;

import com.ptswitch.material.model.material.Material;
import com.ptswitch.material.pages.base.MaterialBasePage;
import com.ptswitch.material.service.MaterialService;
import com.ptswitch.material.util.MaterialUtil;

public class LikeMaterial extends MaterialBasePage {
    @Inject
    private Logger logger;
    @Inject
    private MaterialService materialSer;
    @Property
    private Material material;
    @Property
    private List<Material> list;
    @Property
    private SplitPageInfo page = new SplitPageInfo();

    // init()->initalize->onPassivate->init(xxx)->display
    public Object initialize(Material material, long size, long first) {
        this.material = material;
        page.setPageSize(size);
        page.setCurrentPage(first);
        return this;
    }

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init() {
        return null;
    }

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(String code, String name, String spec, String desc, String pcb, String state, String foot, String brand, String supplier, String remark, long size, long first) {
        this.material = MaterialUtil.toMaterial(code, name, spec, desc, pcb, state, foot, brand, supplier, remark);
        page.setPageSize(size);
        page.setCurrentPage(first);
        return null;
    }

    @SetupRender
    Object display() {
        if (material != null) {
            page.setRowCount(materialSer.countLikeByExample(material));
            list = materialSer.findLikeByExample(material, page.getCurrentRow(), page.getPageSize());
        }
        return null;
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return MaterialUtil.toUrl(material, page.getPageSize(), page.getCurrentPage());
    }
}
